const mysql = require('mysql2');

const db = mysql.createPool({
  host: 'localhost',
  user: 'root',
  password: '11252711zY',
  database: 'nodeExpress'
})

// 测试连接
db.query('SELECT * FROM userInfo', (err, results) => {
  if (err) {
    console.error('Database connection failed:', err);
  } else {
    console.log('Database connected successfully.');
    console.log(results);
  }
});

const user1 = { name: 'wxl', age: 21, gender:'女', create_time: new Date() };

const sqlStr = 'insert into userInfo (name,age,gender,create_time) values (?,?,?,?)';

db.query(sqlStr,[user1.name,user1.age,user1.gender,user1.create_time], (err,results) => {
  if(err){
    return console.log('插入语句失败',err.message)
  }else {
    console.log('插入语句成功',results)
  }
})


db.query('select count(*) from userInfo where age > ? and gender = ?', [20, '女'], (err,result)=>{
  if(err){
    return console.log('查询语句失败',err.message)
  }else {
    console.log('查询语句成功',result)
  }
})